package cn.telling.common.tools.message.dao.impl;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import cn.telling.common.pager.PageVo;
import cn.telling.common.pager.Pager;
import cn.telling.common.tools.message.dao.IMessageInfoDao;
import cn.telling.common.tools.message.util.MessageConstantValues;
import cn.telling.common.tools.message.vo.MessageInfoVo;
import cn.telling.common.util.Common;
import cn.telling.utils.AutoInjection;
import cn.telling.utils.StringHelperTools;
/**   
 * @Title: MessageInfoDaoImpl.java 
 * @Package com.ordermanager.dao.impl 
 * @Description: TODO(描述该文件做什么) 
 * @author 薛松坛   
 * @date 2013-3-28 上午9:08:43 
 * @version V1.0   
 */
@Repository("messageInfoDao")
public class MessageInfoDaoImpl implements IMessageInfoDao {
	private static Logger logger = Logger.getLogger(MessageInfoDaoImpl.class);   
	
	@Autowired
    @Qualifier("film-template")
    private JdbcTemplate jdbcTemplate;
	
	/**
	 * @Description: 新增消息。
	 * @param		参数说明
	 * @return		返回值
	 * @author      薛松坛
	 * @date 2013-3-28 上午10:59:12 
	 * @version V1.0
	 */
	@Override
	public int insertMessageInfo(MessageInfoVo vo) {
		BigDecimal to_user_id = vo.getTo_user_id();//消息对应人ID
		String tableName = Common.getTableNum(to_user_id,4,"message_info");
		StringBuffer strBuffer = new StringBuffer();
		strBuffer.append(" insert into "+tableName+"(id,detail_id,send_user_id,to_user_id,message_memo,send_time, ");
		strBuffer.append(" message_type,message_status,read_status,del_flag) ");
		strBuffer.append(" values(seq_"+tableName+"_pk.Nextval,?,?,?,?,sysdate,?,?,?,?) ");
		Object[] param = new Object[]{
				vo.getDetail_id(),
				vo.getSend_user_id(),
				vo.getTo_user_id(),
				vo.getMessage_memo(),
				vo.getMessage_type(),
				MessageConstantValues.MESSAGE_STATUS_01,
				MessageConstantValues.MESSAGE_READ_STATUS_01,
				MessageConstantValues.MESSAGE_DEL_FLAG_1
		};
		logger.debug("消息新增查询: " + Common.logSQL(strBuffer.toString(),StringHelperTools.nullToString(param))); 
		int i = jdbcTemplate.update(strBuffer.toString(),StringHelperTools.nullToString(param));
		return i;
	}
	/**
	 * @Description: 获取当前登录人未读消息列表。
	 * @param		参数说明
	 * @return		当前登录人未读消息列表
	 * @author      薛松坛
	 * @date 2013-3-26 下午4:47:28 
	 * @version V1.0
	 */
	@Override
    public List<MessageInfoVo> getMessageInfoLs(MessageInfoVo vo,PageVo pageVo){
		BigDecimal to_user_id = vo.getTo_user_id();//消息对应人ID
		String SQL = getMessageSQL(vo,true);
 		if(pageVo!=null){
			SQL = Pager.getPageDatas(pageVo,SQL);
		}
		Object[] param = new Object[]{to_user_id};
		logger.debug("当前登录消息列表查询: " + Common.logSQL(SQL,StringHelperTools.nullToString(param))); 
		List<MessageInfoVo> ls = (List<MessageInfoVo>) jdbcTemplate.query(
				SQL,StringHelperTools.nullToString(param), new RowMapper<MessageInfoVo>() {
					public MessageInfoVo mapRow(ResultSet rs, int i) throws SQLException {
						MessageInfoVo vo = new MessageInfoVo();
						AutoInjection.Rs2Vo(rs, vo, null);
						return vo;
					}
				});
		return ls;
	}
	/**
	 * @Description:按条件查询当前登录人未读信息数量。
	 * @param		参数说明
	 * @return		当前登录人未读信息数量
	 * @author      薛松坛
	 * @date 2013-3-26 下午4:47:28 
	 * @version V1.0
	 */
	@Override
	public int getNotReadMsgCount(MessageInfoVo vo){
		BigDecimal to_user_id = vo.getTo_user_id();//消息对应人ID
		String SQL = getMessageSQL(vo,false);
		SQL = Pager.getTotalPageSql(null,SQL);
		Object[] param = new Object[]{to_user_id};
		logger.debug("我的未读消息总数查询: " + Common.logSQL(SQL,StringHelperTools.nullToString(param))); 
		int count = jdbcTemplate.queryForInt(SQL,StringHelperTools.nullToString(param));
		return count;
	}
	/**
	 * @Description:按条件查询当前登录人信息数量。
	 * @param		参数说明
	 * @return		当前登录人未读信息数量
	 * @author      薛松坛
	 * @date 2013-3-26 下午4:47:28 
	 * @version V1.0
	 */
	@Override
	public int getMessageCount(MessageInfoVo vo){
		BigDecimal to_user_id = vo.getTo_user_id();//消息对应人ID
		String SQL = getMessageSQL(vo,true);
		SQL = Pager.getTotalPageSql(null,SQL);
		Object[] param = new Object[]{to_user_id};
		logger.debug("我的消息总数查询: " + Common.logSQL(SQL,StringHelperTools.nullToString(param))); 
		int count = jdbcTemplate.queryForInt(SQL,StringHelperTools.nullToString(param));
		return count;
	}
    /**
     * @Description:查询当前登录人信息列表。
     * @param		vo 消息内容
     * @param       readStatus true 未读  false内容
     * @return		当前登录人未读信息SQL
     * @author      薛松坛
     * @date 2013-3-28 下午2:03:37 
     * @version V1.0
     */
    private String getMessageSQL(MessageInfoVo vo,boolean readStatus){
    	BigDecimal to_user_id = vo.getTo_user_id();//消息对应人ID
		String tableName = Common.getTableNum(to_user_id,4,"message_info");
    	StringBuffer strBuffer = new StringBuffer();
		strBuffer.append(" select mi.id,mi.detail_id,mi.send_user_id,mi.to_user_id,mi.message_memo,to_char(mi.send_time,'yyyy-mm-dd hh24:mi:ss') send_time,");
		strBuffer.append(" mi.message_type,mi.message_status,mi.read_status,mi.del_flag,mi.bak_1,mi.bak_2,mi.bak_3,mi.bak_4,mi.bak_5,mi.bak_6 ");
		strBuffer.append(" from  "+tableName+" mi  where mi.del_flag = '1' ");
		if(!readStatus){
			strBuffer.append(" and mi.read_status = '"+MessageConstantValues.MESSAGE_READ_STATUS_01+"'  ");
		}
		strBuffer.append(" and mi.to_user_id = ? order by mi.send_time desc ");
		return strBuffer.toString();
    }
	/**
	 * @Description:更新消息读取状态为是
	 * @param		参数说明
	 * @return		消息更新标示
	 * @author      薛松坛
	 * @date 2013-3-27 下午3:31:41 
	 * @version V1.0
	 */
	@Override
	public int updateMessageInfo(MessageInfoVo vo){
    	BigDecimal to_user_id = vo.getTo_user_id();//消息对应人ID
		String tableName = Common.getTableNum(to_user_id,4,"message_info");
		BigDecimal id = vo.getId();
		String SQL1 = " update "+tableName+" mi set mi.read_status = ? where mi.id = ? ";
		Object[] param = new Object[]{
				MessageConstantValues.MESSAGE_READ_STATUS_02,
				id
		};
		logger.debug("消息更新: " + Common.logSQL(SQL1,StringHelperTools.nullToString(param))); 
		int i = jdbcTemplate.update(SQL1,StringHelperTools.nullToString(param));
		return i;
	}
	/**
	 * @Description:更新删除标示为是
	 * @param		参数说明
	 * @return		消息更新标示
	 * @author      薛松坛
	 * @date 2013-3-27 下午3:31:41 
	 * @version V1.0
	 */
	@Override
	public int deleteMessageInfo(MessageInfoVo vo){
    	BigDecimal to_user_id = vo.getTo_user_id();//消息对应人ID
		String tableName = Common.getTableNum(to_user_id,4,"message_info");
		BigDecimal id = vo.getId();
		String SQL1 = " update "+tableName+" mi set mi.del_flag = ? where mi.id = ? ";
		Object[] param = new Object[]{
				MessageConstantValues.MESSAGE_DEL_FLAG_0,
				id
		};
		logger.debug("更新删除标示为是: " + Common.logSQL(SQL1,StringHelperTools.nullToString(param))); 
		int i = jdbcTemplate.update(SQL1,StringHelperTools.nullToString(param));
		return i;
	}
	/**
	 * 批量更新消息信息
	 * @param updateLst
	 * @return
	 */
	@Override
	public int updateMessageInfoBatch(List<MessageInfoVo> updateLst){
		BigDecimal to_user_id = updateLst.get(0).getTo_user_id();//消息对应人ID
		String tableName = Common.getTableNum(to_user_id,4,"message_info");
		StringBuffer sql = new StringBuffer();
		sql.append("update "+tableName+" mi "
		     + "	set mi.read_status = ? "
		     + "	where mi.id = ? " ); 
		int[] i = jdbcTemplate.batchUpdate(sql.toString(),new UpdateMessageInfoVoPSSetter(updateLst));
		return i.length;
		
	}
	/**
	 * 批量更新消息信息回调类
	 */
	private class UpdateMessageInfoVoPSSetter implements BatchPreparedStatementSetter {
		final List<MessageInfoVo>  temList;
		/** 通过构造函数把要插入的数据传递进来处理 */
		public UpdateMessageInfoVoPSSetter(List<MessageInfoVo>  list) {
			temList = list;
		}
		public int getBatchSize() {
			return temList.size();
		}
		public void setValues(PreparedStatement ps, int i) throws SQLException {
			MessageInfoVo vo = (MessageInfoVo) temList.get(i);
			ps.setString(1,MessageConstantValues.MESSAGE_READ_STATUS_02);
			ps.setBigDecimal(2,vo.getId());
		}
	}
	/**
	 * 批量更新删除标示为是
	 * @param deleteLst
	 * @return
	 */
	@Override
	public int deleteMessageInfoBatch(List<MessageInfoVo> deleteLst){
		BigDecimal to_user_id = deleteLst.get(0).getTo_user_id();//消息对应人ID
		String tableName = Common.getTableNum(to_user_id,4,"message_info");
		StringBuffer sql = new StringBuffer();
		sql.append("update "+tableName+" mi "
		     + "	set mi.del_flag = ? "
		     + "	where mi.id = ? " ); 
		int[] i = jdbcTemplate.batchUpdate(sql.toString(),new deleteMessageInfoVoPSSetter(deleteLst));
		return i.length;
		
	}
	/**
	 * 批量更新删除标示为是回调类
	 */
	private class deleteMessageInfoVoPSSetter implements BatchPreparedStatementSetter {
		final List<MessageInfoVo>  temList;
		/** 通过构造函数把要插入的数据传递进来处理 */
		public deleteMessageInfoVoPSSetter(List<MessageInfoVo>  list) {
			temList = list;
		}
		public int getBatchSize() {
			return temList.size();
		}
		public void setValues(PreparedStatement ps, int i) throws SQLException {
			MessageInfoVo vo = (MessageInfoVo) temList.get(i);
			ps.setString(1,MessageConstantValues.MESSAGE_DEL_FLAG_0);
			ps.setBigDecimal(2,vo.getId());
			

		}
	}
}
